package com.wyw.minio.learning.utils;

import cn.hutool.core.io.FileTypeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Title   文件类型工具类
 * @Description
 * @Author Mr Wu yewen.wu.china@gmail.com
 * @Date 2024/3/28 16:51
 * Update History:
 * Author        Time            Content
 */
@Slf4j
public class FileTypeUtils {

    private final static String IMAGE_TYPE = "image/";
    private final static String AUDIO_TYPE = "audio/";
    private final static String VIDEO_TYPE = "video/";
    private final static String APPLICATION_TYPE = "application/";
    private final static String TEXT_TYPE = "text/";


    public static String getFileType(MultipartFile multipartFile) {
        InputStream inputStream = null;
        String type = null;
        try {
            inputStream = multipartFile.getInputStream();
            type = FileTypeUtil.getType(inputStream);

            if (type.equalsIgnoreCase("JPG") || type.equalsIgnoreCase("JPEG") ||
                    type.equalsIgnoreCase("BMP") || type.equalsIgnoreCase("PCX") ||
                    type.equalsIgnoreCase("TGA") || type.equalsIgnoreCase("PSD") ||
                    type.equalsIgnoreCase("TIFF") || type.equalsIgnoreCase("PNG")
            ) {
                return IMAGE_TYPE + type;
            }
            if (type.equalsIgnoreCase("MP3") || type.equalsIgnoreCase("OGG") ||
                    type.equalsIgnoreCase("WAV") || type.equalsIgnoreCase("REAL") ||
                    type.equalsIgnoreCase("APE") || type.equalsIgnoreCase("MODULE") ||
                    type.equalsIgnoreCase("MIDI") || type.equalsIgnoreCase("VQF") ||
                    type.equalsIgnoreCase("CD")
            ) {
                return AUDIO_TYPE + type;
            }
            if (type.equalsIgnoreCase("MP4") || type.equalsIgnoreCase("AVI") ||
                    type.equalsIgnoreCase("MPEG-1") || type.equalsIgnoreCase("RM") ||
                    type.equalsIgnoreCase("ASF") || type.equalsIgnoreCase("WMV") ||
                    type.equalsIgnoreCase("QLV") || type.equalsIgnoreCase("MPEG-2") ||
                    type.equalsIgnoreCase("MPEG4") || type.equalsIgnoreCase("MOV") ||
                    type.equalsIgnoreCase("3GP")
            ) {
                return VIDEO_TYPE + type;
            }
            if (type.equalsIgnoreCase("DOC") || type.equalsIgnoreCase("DOCX") ||
                    type.equalsIgnoreCase("PPT") || type.equalsIgnoreCase("PPTX") ||
                    type.equalsIgnoreCase("XLS") || type.equalsIgnoreCase("XLSX") ||
                    type.equalsIgnoreCase("ZIP") || type.equalsIgnoreCase("JAR")
            ) {
                return APPLICATION_TYPE + type;
            }
            if (type.equalsIgnoreCase("TXT")) {
                return TEXT_TYPE + type;
            }
        } catch (IOException e) {
            log.error("文件类型处理异常", e);
        }
        return null;
    }

}
